const validator = require("./validator");

const requestUtil = require("./req.util");

const yx2RequestUtil = require("./http/http");

const config = require("./apiurl"); 


var httpRequestTemp = requestUtil.apphttp;
export const KEY_API_PLACEHODLER = "KEY_API_PLACEHODLER";
const KEY_DYNATIC_SERVICE = "KEY_DYNATIC_SERVICE";

export const serviceList = [{
	name: config.apiUrl_trial.apiUrl
}, {
	name: 'https://dorm-api.hichain.com'
}, {
	name: 'https://api.yx2flat.com'
}, {
	name: 'http://192.168.1.16:10001/api'
}, {
	name: 'http://192.168.1.27:8100'
}, {
	name: 'https://122.233.163.138:8199'
}, {
	name: 'https://ssltest.woniugm.com'
}, {
	name: 'https://ssl-test-html.woniugm.com'
}, {
	name: 'http://192.168.1.127:8100'
}, {
	name: 'http://192.168.1.229:8100'
}, {
	name: 'http://192.168.1.128:8200'
}, {
	name: 'http://192.168.1.128:8100'	
}, {
	name: 'http://192.168.1.229:8100'
}, {
	name: 'http://192.168.1.229:8101'
}, {
	name: 'http://192.168.1.247:8100'
}, {
	name: 'http://192.168.1.126:8100'
}, {
	name: 'http://192.168.1.220:8100'
}, {
	name: 'http://192.168.1.220:8081/api'
}, {
	name: 'http://192.168.1.220:8101'
}, {
	name: 'http://192.168.1.220:8081/api'
}, {
	name: 'http://192.168.1.77:10001/api'
}, {
	name: 'http://192.168.1.16:8100'
}, {
	name: 'http://192.168.1.27:8100'
}, {
	name: 'http://192.168.1.121:8100'
}, {
	name: 'https://apitest.woniugm.com'
}, {
	name: 'http://192.168.1.230:8100'
}, {
	name: 'http://115.204.143.35:8101'
}, {
	name: 'http://115.204.143.35:8100'
}, {
	name: 'https://api.wuye.50bm.com'
}];
export const loadAddress = () => {
	if (validator.isNullOrEmpty(getApp())) {
		var service = uni.getStorageSync(KEY_DYNATIC_SERVICE);

		if (validator.isNullOrEmpty(service)) {
			service = serviceList[0].name;
			uni.setStorageSync(KEY_DYNATIC_SERVICE, service);
		}

		return service;
	} else {
		if (!validator.isNullOrEmpty(getApp().globalData) && !validator.isNullOrEmpty(getApp()
				.globalData.service)) return getApp().globalData.service;
		var service = uni.getStorageSync(KEY_DYNATIC_SERVICE);

		if (validator.isNullOrEmpty(service)) {
			service = serviceList[0].name;
			uni.setStorageSync(KEY_DYNATIC_SERVICE, service);
		}

		getApp().globalData.service = service;
		return service;
	}
};
export const changeAddress = service => {
	if (validator.isNullOrEmpty(service)) return;
	uni.setStorageSync(KEY_DYNATIC_SERVICE, service);
	getApp().globalData.service = service;
};
/**
 * 给下钩子
 * @param {*} url 
 * @param {*} data 
 * @param {*} options 
 */

export const hook_request = () => {
	if (requestUtil.apphttp !== inner_http_request) {
		requestUtil.apphttp = inner_http_request;
	}

	if (yx2RequestUtil.request !== inner_http_request) {
		yx2RequestUtil.request = inner_http_request;
	}
};
/**
 * 内部网络请求
 * @param {*} url 
 * @param {*} data 
 * @param {*} options 
 */

const inner_http_request = (url, data = {}, options = {}) => {
	// 替换占位符
	var address = loadAddress();
	url = url.replace(KEY_API_PLACEHODLER, address);
	return httpRequestTemp(url, data, options);
};
/**
 * 索引值
 */


export const indexOfAddress = address => {
	for (var i = 0; i < serviceList.length; i++) {
		var service = serviceList[i].name;

		if (service === address) {
			return i;
		}
	}

	return 0;
};
